﻿#include "LinkedList.h"
#include<stdexcept>
#include<iostream>

Linkedlist::Linkedlist(int x) :val(x), next(nullptr) {

}
Linkedlist* Linkedlist::GetNext() {
	return this->next;
}
void Linkedlist::SetNext(int _val) {
	Linkedlist* node = new Linkedlist(_val);
	this->next = node;
}

void Linkedlist::Insert(Linkedlist* node, Linkedlist* p) {
	//获得当前节点的next节点
	Linkedlist* next = node->next;
	//将当前节点的next节点指向新节点
	node->next = p;
	//将新节点的next节点指向原来的next节点
	p->next = next;
}
void Linkedlist::Delete(Linkedlist* node) {
	if (node->next == nullptr)
		return;
	Linkedlist* delNode = node->next;	
	Linkedlist* nextNode = delNode->next;
	node->next = nextNode;
	//释放内存
	delete delNode;
}
Linkedlist* Linkedlist::Get(Linkedlist* head, int index) {
	if (head == nullptr)
		throw std::out_of_range("linkedlist is empty");
	for (int i = 0; i < index; i++) {
		head = head->next;
	}
	return head;
}
int Linkedlist::Find(Linkedlist* head, int findValue) {
	int index = 0;
	while (head != nullptr) {
		if (head->val == findValue)
			return index;
		head = head->next;
		index++;
	};
	return -1;
}
void Linkedlist::Test() {
	std::cout << "From 05_linkedlist.cpp" << std::endl;
}